package com.ruoyi.controller.jpjs.costcode;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.service.SysParameterService;
import com.ruoyi.service.jpjs.costcode.CostCodeService;
import com.ruoyi.service.targets.TargetInfoService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.util.ExcelUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/costCode")
public class CostCodeController extends BaseController {

    @Autowired
    private CostCodeService costCodeService;

    @Autowired
    private SysParameterService sysParameterService;

    private String prefix = "jpjs/costcode";

    @GetMapping()
    public String list(ModelMap mmp) {
        return prefix + "/CostCode";
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<Map<String, Object>> list(@RequestParam Map<String, Object> map) {
        return costCodeService.findCostCodeAll(map);
    }

    @GetMapping("/add/{codeId}")
    public String add(@PathVariable("codeId") String codeId, ModelMap mmp) {
        mmp.put("parentId", codeId);
        mmp.put("codeUnit", sysParameterService.findNameByType("成本科目编码单位"));
        return prefix + "/addCostCode";
    }

    @GetMapping("/edit/{codeId}")
    public String edit(@PathVariable("codeId") String codeId, ModelMap mmp) {
        mmp.put("codeUnit", sysParameterService.findNameByType("成本科目编码单位"));
        mmp.put("costCode", costCodeService.findCostCode(codeId));
        return prefix + "/editCostCode";
    }

    @PostMapping("/checkCodeNumUnique")
    @ResponseBody
    public int checkCodeNumUnique(@RequestParam Map<String, Object> map) {
        return costCodeService.checkCodeNumUnique(map);
    }

    @PostMapping("/save")
    @ResponseBody
    public AjaxResult saveCostCode(@RequestParam Map<String, Object> map) {
        return toAjax(costCodeService.saveCostCode(map));
    }

    @GetMapping("/remove/{codeId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("codeId") String codeId) {
        if (costCodeService.findChildLevel(codeId) > 0) {
            return AjaxResult.warn("存在下级区域,不允许删除");
        }
        return toAjax(costCodeService.deleteCostCode(codeId));
    }

    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) {
        return toAjax(costCodeService.importData(file));
    }

}
